package edu.cebanc.spring.biblioteca.services;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import edu.cebanc.spring.biblioteca.bean.BookBean;
import edu.cebanc.spring.biblioteca.bean.BookCopyBean;
import edu.cebanc.spring.biblioteca.dao.BookCopiesDAO;
import edu.cebanc.spring.biblioteca.dao.BooksDAO;
import edu.cebanc.spring.biblioteca.domain.Book;
import edu.cebanc.spring.biblioteca.domain.BookCopy;
import edu.cebanc.spring.biblioteca.util.DataParser;

@Service("bookCopiesService")
@Transactional(propagation= Propagation.SUPPORTS, readOnly = true)
public class BookCopiesServiceImpl implements BookCopiesService {

	@Autowired
	private BookCopiesDAO bookCopiesDAO;
	
	@Override
	@Transactional(propagation= Propagation.REQUIRED, readOnly = false)
	public void saveBookCopy(BookCopyBean bookCopy) {
		DataParser parser = new DataParser();
		bookCopiesDAO.saveBookCopy(parser.BookCopyBean2BookCopy(bookCopy));		
	}

	@Override
	public List<BookCopyBean> listBookCopies() {
		DataParser parser = new DataParser();
		return parser.BookCopyList2BookCopyBeanList(bookCopiesDAO.listBookCopies());
	}

	@Override
	public BookCopyBean loadBookCopy(String id) {
		DataParser parser = new DataParser();
		BookCopy b = bookCopiesDAO.getBookCopy(Integer.parseInt(id));
		return parser.BookCopy2BookCopyBean(b);
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED, readOnly = false)
	public void deleteBookCopy(String id) {
		bookCopiesDAO.delBookCopy(bookCopiesDAO.getBookCopy(Integer.parseInt(id)));
		
	}
	
}
